package com.wyq.miaosha.controller;

import com.wyq.miaosha.domain.MiaoshaUser;
import com.wyq.miaosha.redis.GoodsKey;
import com.wyq.miaosha.redis.RedisService;
import com.wyq.miaosha.result.Result;
import com.wyq.miaosha.service.GoodsService;
import com.wyq.miaosha.vo.GoodsDetailVo;
import com.wyq.miaosha.vo.GoodsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.IWebContext;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	@Autowired
	GoodsService goodsService;
	@Autowired
	RedisService redisService;
	@Autowired
	ThymeleafViewResolver thymeleafViewResolver;

	@ResponseBody
    @RequestMapping("/to_list")
    public String list(HttpServletRequest request,
					   HttpServletResponse response,
					   Model model, MiaoshaUser user) {
    	model.addAttribute("user", user);

		//取缓存
		String html = redisService.get(GoodsKey.getGoodsList,"",String.class);
		if(!StringUtils.isEmpty(html)) {
			return html;
		}

		//数据库查询
		List<GoodsVo> goodsList = goodsService.listGoodsVo();
		model.addAttribute("goodsList", goodsList);

		//手动渲染
		IWebContext ctx =new WebContext(request,response,
											request.getServletContext(),
											request.getLocale(),model.asMap());
		html = thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx);
		if(!StringUtils.isEmpty(html)){
			redisService.set(GoodsKey.getGoodsList,"",html);
		}
		return html;
    }

	@ResponseBody
	@RequestMapping("/to_detail/{goodsId}")
	public String detail(HttpServletRequest request,
						 HttpServletResponse response,
						 Model model, MiaoshaUser user,@PathVariable("goodsId") long id) {
		model.addAttribute("user", user);

		// 获取缓存
		String html = redisService.get(GoodsKey.getGoodsDetail,""+id,String.class);
		if(!StringUtils.isEmpty(html)){
			return html;
		}
		//数据库查询
		GoodsVo goods = goodsService.getGoodsVoByGoodsId(id);
		model.addAttribute("goods", goods);

		long startAt = goods.getStartDate().getTime();
		long endAt = goods.getEndDate().getTime();
		long now = System.currentTimeMillis();

		int miaoshaStatus = 0;
		int remainSeconds = 0;
		if(now < startAt ) {//秒杀还没开始，倒计时
			miaoshaStatus = 0;
			remainSeconds = (int)((startAt - now )/1000);
		}else  if(now > endAt){//秒杀已经结束
			miaoshaStatus = 2;
			remainSeconds = -1;
		}else {//秒杀进行中
			miaoshaStatus = 1;
			remainSeconds = 0;
		}
		model.addAttribute("miaoshaStatus", miaoshaStatus);
		model.addAttribute("remainSeconds", remainSeconds);

		//读取静态页面存储到redis
		IWebContext ctx = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
		html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", ctx);
		if(!StringUtils.isEmpty(html)){
			redisService.set(GoodsKey.getGoodsDetail,""+id,String.class);
		}
		return html;
	}

	@RequestMapping(value="/detail/{goodsId}")
	@ResponseBody
	public Result<GoodsDetailVo> detail2(HttpServletRequest request, HttpServletResponse response, Model model,MiaoshaUser user,
										@PathVariable("goodsId")long goodsId) {
		GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
		long startAt = goods.getStartDate().getTime();
		long endAt = goods.getEndDate().getTime();
		long now = System.currentTimeMillis();
		int miaoshaStatus = 0;
		int remainSeconds = 0;
		if(now < startAt ) {//秒杀还没开始，倒计时
			miaoshaStatus = 0;
			remainSeconds = (int)((startAt - now )/1000);
		}else  if(now > endAt){//秒杀已经结束
			miaoshaStatus = 2;
			remainSeconds = -1;
		}else {//秒杀进行中
			miaoshaStatus = 1;
			remainSeconds = 0;
		}
		GoodsDetailVo vo = new GoodsDetailVo();
		vo.setGoods(goods);
		vo.setUser(user);
		vo.setRemainSeconds(remainSeconds);
		vo.setMiaoshaStatus(miaoshaStatus);
		return Result.success(vo);
	}
    
}
